local liezhi = fk.CreateSkill {
    name = "sp__liezhi",
}

Fk:loadTranslationTable{
    ["sp__liezhi"] = "烈直",
    [":sp__liezhi"] = "你可弃置一名角色的一张牌，以将你的最后一张手牌当任意本回合未以此法转化过的类型的基本牌或普通锦囊牌使用或打出。",

    ["#liezhi"] = "烈直：你可以弃置一名角色的一张牌，以将你的最后一张手牌当任意本回合未以此法转化过的类型的基本牌或普通锦囊牌使用或打出。",
    ["#liezhi_choose1"] = "烈直：选择一名角色以弃置其一张牌",
    ["#liezhi_choose2"] = "烈直：弃置一张牌并视为使用一张基本牌或普通锦囊牌",

    ["@liezhibasic-turn"] = "基本 已使用",
    ["@liezhitrick-turn"] = "锦囊 已使用"
}


liezhi:addEffect("viewas", {
    pattern = ".|.|.|.|.|basic,trick",
    prompt = "#liezhi",
    interaction = function(self, player)
        local all_cards = Fk:getAllCardIds()
        local all_names = Fk:getAllCardNames("bt")
        local names = player:getViewAsCardNames(liezhi.name, all_names)
        if player:getMark("@liezhibasic-turn") > 0 then
            local all_names_b = Fk:getAllCardNames("b")
            for _, i in ipairs(all_names_b) do
                if (table.contains(names, i) == true) then
                    table.removeOne(names, i)
                end
            end
        end
        if player:getMark("@liezhitrick-turn") > 0 then
            local all_names_t = Fk:getAllCardNames("t")
            for _, i in ipairs(all_names_t) do
                if (table.contains(names, i) == true) then
                    table.removeOne(names, i)
                end
            end
        end
        if #names == 0 then return end
        return UI.CardNameBox {choices = names, all_choices = all_names}
    end,
    handly_pile = true,
    card_filter = function(self, player, to_select, selected)
        return #selected == 0 and table.contains(player:getCardIds("h"), to_select) == true
    end,
    view_as = function(self, player, cards)
        if #cards ~= 1 or not self.interaction.data then return end
        local card = Fk:cloneCard(self.interaction.data)
        card:setMark("@liezhi_use-inhand", cards[1])
        card:addSubcards(cards)
        card.skillName = liezhi.name
        return card
    end,
    before_use = function (self, player, use)
        local room = player.room
        local tgt
        if (#player:getCardIds("h") == 2) then tgt = {player}
        else
            local all_players = room:getOtherPlayers(player, false)
            local allplayers = table.filter(all_players, function (p) return #p:getCardIds("he") > 0 end)
            tgt = room:askToChoosePlayers(player, {
                min_num = 1,
                max_num = 1,
                targets = allplayers,
                skill_name = liezhi.name,
                prompt = "#liezhi_choose1"
            })
        end
        local card
        if (tgt[1] ~= player) then
            card = room:askToChooseCards(player, {
                min = 1,
                max = 1,
                flag = "he",
                target = tgt[1],
                skill_name = liezhi.name,
                prompt = "#liezhi_choose2"
            })
        else 
            local cards = player:getCardIds("h")
            local cardid = table.filter(cards, function (p) return use.card:getMark("@liezhi_use-inhand") ~= p end)
            card = Fk:getCardById(cardid[1])
        end
        room:throwCard(card, liezhi.name, tgt[1], player)
        if use.card.type == Card.TypeBasic then
            room:setPlayerMark(player, "@liezhibasic-turn", 1)
        else
            room:setPlayerMark(player, "@liezhitrick-turn", 1)
        end
    end,
    enabled_at_play = function(self, player)
        local flag = false
        if (#player:getCardIds("h") == 1) then
            local all_players = Fk:currentRoom().alive_players
            local allplayers = table.filter(all_players, function (p) return #p:getCardIds("he") > 0 and p ~= player end)
            if #allplayers > 0 then flag = true end
        end
        if (#player:getCardIds("h") == 2) then flag = true end
        return flag
    end,
    enabled_at_response = function(self, player, response)
        local flag = false
        if (#player:getCardIds("h") == 1) then
            local all_players = Fk:currentRoom().alive_players
            local allplayers = table.filter(all_players, function (p) return #p:getCardIds("he") > 0 and p ~= player end)
            if #allplayers > 0 then flag = true end
        end
        if (#player:getCardIds("h") == 2) then flag = true end
        return flag
    end,
})

return liezhi